package com.aaa.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aaa.account.entity.TbAccAccount;
import com.aaa.account.dao.TbAccAccountMapper;
import com.aaa.account.service.TbAccAccountService;
@Service
public class TbAccAccountServiceImpl extends ServiceImpl<TbAccAccountMapper, TbAccAccount> implements TbAccAccountService{

    private Logger logger = LoggerFactory.getLogger(TbAccAccountServiceImpl.class);

    @Resource
    TbAccAccountMapper tbAccAccountMapper;

    public List<TbAccAccount> listByPhone(String phone) {
        QueryWrapper<TbAccAccount> travelsQueryWrapper = new QueryWrapper<>();
        travelsQueryWrapper.like("phone", phone);
        return this.list(travelsQueryWrapper);
    }

    public boolean updatePicpath(String defaultPic, String accountId) {
        return  tbAccAccountMapper.updatePicpath(defaultPic,accountId);
    }

    public List<TbAccAccount> listDesc() {
        QueryWrapper<TbAccAccount> tbPrdInfoQueryWrapper = new QueryWrapper<>();
        tbPrdInfoQueryWrapper.orderByDesc("update_date");
        return this.list(tbPrdInfoQueryWrapper);
    }

    public boolean registered(String phone, String password) {
        TbAccAccount tbAccAccount = new TbAccAccount();

        //把密码转换为密钥
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);

        tbAccAccount.setPhone(phone);
        tbAccAccount.setPassword(encode);
        QueryWrapper<TbAccAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        List<TbAccAccount> tbAccAccounts = tbAccAccountMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(tbAccAccounts)){
            logger.warn("{}-用户已存在，请选择其它用户名!",phone);
            return false;
        }

        int insert = tbAccAccountMapper.insertSelective(tbAccAccount);
        logger.info("{}注册成功！",phone);
        return insert > 0 ? true : false;
    }

}
